/**
 * Captain
 * © 2018 Captain copyright，All rights reserved.
 * http://www.sccaptain.com.cn
 * 
 * JAVA : 8
 * 文  件  名: CommInfoServiceImpl.java
 * 创  建  人: QinYoubin (qin.youbin@sccaptain.com.cn)
 * 创建时间: 2018年4月26日 上午9:50:25
 * 版         本: 1.0.0
 * 备         注:
 * 修订历史:
 */
package com.material.c2c.commodity.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.cpt.framework.core.OperationResult;
import com.material.c2c.classify.dao.ClassifyDao;
import com.material.c2c.classify.entity.SPFL;
import com.material.c2c.commodity.config.CommStatus;
import com.material.c2c.commodity.dao.CommInfoDao;
import com.material.c2c.commodity.dao.CommIntroDao;
import com.material.c2c.commodity.entity.SPJS;
import com.material.c2c.commodity.entity.SPXX;
import com.material.c2c.commodity.model.CommDetailDTO;
import com.material.c2c.core.UUIDGenerator;
import com.material.c2c.img.dao.ImageDao;
import com.material.c2c.img.dao.ImageInfoMapDao;
import com.material.c2c.img.entity.TPXX;
import com.material.c2c.img.entity.TP_XX_MAP;

/**
 * 商品基本信息
 * @since 1.0
 * @version 1.0
 * @author QinYoubin (qin.youbin@sccaptain.com.cn)
 */
@Service
public class CommInfoServiceImpl implements CommInfoService{
  @Resource
  private CommInfoDao commInfo;
  @Resource
  private CommIntroDao commIntroDao;
  @Resource
  private ImageDao imageDao;
  @Resource
  private ImageInfoMapDao imageInfoMapDao;
  @Resource
  private ClassifyDao classifyDao;
  @Override
  @Transactional
  public OperationResult add(SPXX spxx, SPJS spjs, List<TPXX> tpList) {
    // 添加基础信息
    String spid = UUIDGenerator.generateUUIDStr();
    spxx.setId(spid);
    spxx.setLjpjsl(0);
    spxx.setLjxl(0);
    spxx.setSpzt(CommStatus.NORMAL);
    spxx = commInfo.add(spxx);
    //添加介绍信息
    if(spjs != null) {
	    spjs.setId(UUIDGenerator.generateUUIDStr());
	    spjs.setSpid(spid);
	    commIntroDao.add(spjs);
    }
    if(tpList != null) {
	    for(TPXX tp:tpList) {
	      String tpId = UUIDGenerator.generateUUIDStr();
	      tp.setId(tpId);
	      imageDao.add(tp);
	      TP_XX_MAP tpMap = new TP_XX_MAP();
	      tpMap.setId(UUIDGenerator.generateUUIDStr());
	      tpMap.setTpid(tpId);
	      tpMap.setXxid(spid);
	      imageInfoMapDao.add(tpMap);
	    }
    }
    return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult update(SPXX spxx, SPJS spjs) {
    // 更新商品信息
    spxx = commInfo.update(spxx);
    // 更新商品介绍
    spjs = commIntroDao.update(spjs);
    return OperationResult.SUCCEED;
  }
  @Override
  @Transactional
  public OperationResult delete(String id) {
    // 删除商品信息
    commInfo.delete(id);
    //删除商品结婚搜啊
    commIntroDao.deleteBySpid(id);
    return OperationResult.SUCCEED;
  }
  @Override
  public CommDetailDTO getOne(String spid) {
    SPXX spxx = commInfo.getOne(spid);
    SPJS spjs = commIntroDao.findBySpid(spid);
    List<TPXX> tpList = imageDao.listBySpid(spid);
    return new CommDetailDTO(spxx,spjs,tpList);
  }
  @Override
  public Page<SPXX> list(String key,String fl,Pageable pageable){
    if(hasText(key)&&hasText(fl)) {
      return commInfo.findBySpmcAndSpfl(key, fl, pageable);
    } 
    if(hasText(fl) && !hasText(key)) {
      return commInfo.findBySpfl(fl, pageable);
    }
    return commInfo.findAll(pageable);
  }
  @Override
  public Page<CommDetailDTO> search(String key,String fl,Pageable pageable){
	  Page<SPXX> page =null;
	if(hasText(key)&&!hasText(fl) ) {
		page= commInfo.findBySpmcLike(key, pageable);
    } 
    if(hasText(fl) && !hasText(key)) {
    	page= commInfo.findBySpfl(fl, pageable);
    } 
    List<CommDetailDTO> dtoList;
    if(page != null) {
    	dtoList = this.getDtoList(page.getContent());
    } else {
    	dtoList = new ArrayList<CommDetailDTO>();
    }
    Page<CommDetailDTO> res = new PageImpl<CommDetailDTO>(dtoList,pageable,page.getTotalElements());
    return res;
  }
  @Override
  public List<Map<String,Object>> getIndex(List<String> flList){
	  List<Map<String,Object>> resList= new ArrayList<Map<String,Object>>();
	  for(String fl:flList) {
		  Map<String,Object> resMap = new HashMap<String,Object>();
		  List<SPXX> spList = commInfo.findTop8BySpflLikeAndSpzt(fl+"%", CommStatus.NORMAL);
		  SPFL spfl = classifyDao.getOne(fl);
		  resMap.put("goods", getDtoList(spList));
		  resMap.put("classify", spfl);
		  resMap.put("id", spfl.getId());
		  resList.add(resMap);
	  }
	  return resList;
  }
  @Override
  public List<CommDetailDTO> getFlList(String fl){
	  List<SPXX> spList =commInfo.findTop30BySpfl(fl);
	  return getDtoList(spList);
  }
  @Override
  public List<CommDetailDTO> getTjList() {
	  return getDtoList(commInfo.findTop30BySpzt(CommStatus.PROMOTION));
  }
  @Override
  public List<CommDetailDTO> getJfList() {
	  return getDtoList(commInfo.findTop30BySpzt(CommStatus.INTEGRAL));
  }
  @Override
  public Page<CommDetailDTO> classifyPage(String fl, Pageable page){
	  Page<SPXX> spList = commInfo.findBySpflLike(fl, page);
	  List<CommDetailDTO> dtoList = this.getDtoList(spList.getContent());
	  Page<CommDetailDTO> res = new PageImpl<CommDetailDTO>(dtoList,page,spList.getTotalElements());
	  return res;
  }
  public boolean hasText(String str) {
    if(str == null || "".equals(str)) {
      return false;
    } else {
      return true;
    }
  }
  private List<CommDetailDTO> getDtoList(List<SPXX> spList){
	  List<CommDetailDTO> dtoList = new ArrayList<CommDetailDTO>();
	  for(SPXX sp:spList) {
		  CommDetailDTO dto = new CommDetailDTO();
		  dto.setSpxx(sp);
		  dto.setTpList(imageDao.listBySpid(sp.getId()));
		  dtoList.add(dto);
	  }
	  return dtoList;
  }
}
